// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: linchuan

#include "coin2/exchange/bitmex/api_order/native_rest_async_asio_subscriber.h"

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>

#include "coin2/base/log.h"

namespace coin2::exchange::bitmex::api_order {

void BitmexRestAsyncAsioSubscriber::QueryBalance() {
  auto http_context = rest_client()->QueryMargin();
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&BitmexRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void BitmexRestAsyncAsioSubscriber::QueryPosition() {
  auto http_context = rest_client()->QueryPosition();
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&BitmexRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void BitmexRestAsyncAsioSubscriber::QueryOpenOrders(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  params.set_filter(ApiSignatureUtil::EscapeURL(R"({"ordStatus": "New"})"));
  params.set_reverse("true");
  auto http_context = rest_client()->QueryOrders(params);
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&BitmexRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void BitmexRestAsyncAsioSubscriber::QueryAllOrders(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  params.set_reverse("true");
  params.set_count("20");
  auto http_context = rest_client()->QueryOrders(params);
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&BitmexRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void BitmexRestAsyncAsioSubscriber::QueryFill(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  params.set_reverse("true");
  params.set_count("20");
  auto http_context = rest_client()->QueryExecution(params);
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&BitmexRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void BitmexRestAsyncAsioSubscriber::SubscribeAsync() {
  AddPeriodicTask(
      std::bind(&BitmexRestAsyncAsioSubscriber::QueryBalance, this),
      config_.query_account_balance_period_sec(),
      "query_balance");

  AddPeriodicTask(
      std::bind(&BitmexRestAsyncAsioSubscriber::QueryPosition, this),
      config_.query_account_position_period_sec(),
      "query_position");

  auto symbol_list = native_symbols();

  AddPeriodicTask(
      std::bind(&BitmexRestAsyncAsioSubscriber::QueryOpenOrders, this, std::placeholders::_1),
      config_.query_open_orders_period_sec(),
      "query_open_orders",
      symbol_list);

  AddPeriodicTask(
      std::bind(&BitmexRestAsyncAsioSubscriber::QueryAllOrders, this, std::placeholders::_1),
      config_.query_cancel_orders_period_sec(),
      "query_all_orders",
      symbol_list);

  AddPeriodicTask(
      std::bind(&BitmexRestAsyncAsioSubscriber::QueryFill, this, std::placeholders::_1),
      config_.query_fill_period_sec(),
      "query_fill",
      symbol_list);
}

}  // namespace coin2::exchange::bitmex::api_order
